/**
 * 添加办公中心地址
 */
var key = null;
var modifydata = null;
var _editor = [];

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	init_city( "province", "city" );
	button_bind();
	init_editors();

	// 修改数据设置
	if( modifydata )
		setOfficeData( modifydata );
} );

function button_bind()
{
	$( "#name" ).focus();

	$( "#btnSubmit" ).bind( "click", function()
	{
		submit_form();
	} );

	$( "#restday > span" ).click( function( e )
	{
		$( this ).toggleClass( "selected" );
	} );
}

function setOfficeData( data )
{
	// 客户资料
	if( typeof ( data ) == "object" && data.name )
	{
		key = data.key;

		$( "#name" ).val( data.name );
		$( "#address" ).val( data.address );
		$( "#inphone" ).val( data.inphone );
		$( "#outphone" ).val( data.outphone );
		$( "#citys" ).val( data.citys );

		var days = data.restday.split( "," );

		if( days.length > 0 && !empty( days[ 0 ] ) )
		{
			$( "#restday > span" ).each( function( idx )
			{
				if( days.indexOf( $( this ).attr( "rel" ) ) != -1 )
					$( this ).addClass( "selected" );
			} );
		}
		
		// 所在地
		$( "#province" ).val( data.province );
		update_selected( data.city );

		// 通知
		$( "#notice" ).html( data.notice );
	}
	else
	{
		key = null;

		$( "#name" ).val( "" );
		$( "#address" ).val( "" );
		$( "#inphone" ).val( "" );
		$( "#outphone" ).val( "" );
		$( "#province > option:first" ).attr( "selected", true );
		$( "#city > option:first" ).attr( "selected", true );

		$( "#restday > span.selected" ).each( function( idx )
		{
			$( this ).removeClass( "selected" );
		} );

		// 通知
		$( "#notice" ).html( "" );
	}
}


function submit_form()
{
	var data = {};

	// 客户资料
	data.keyname = $( "#keyname" ).val();
	data.name = $( "#name" ).val();
	data.address = $( "#address" ).val();
	data.inphone = $( "#inphone" ).val();
	data.outphone = $( "#outphone" ).val();
	data.target = $("#target").val();
	data.unum = $("#unum").val();
	// 休息日
	var days = [];
	$( "#restday > span.selected" ).each( function( idx )
	{
		days.push( $( this ).attr( "rel" ) );
	} );
	data.restday = days.join( "," );
	
	// 所在地
	data.province = $( "#province" ).val();
	data.city = $( "#city" ).val();
	data.citys = $( "#citys" ).val();

	// 通知
	data.notice = $( "#notice" ).html();

	// 通知同步标记
	data.synch = $( "#synch" ).prop( "checked" ) ? 1 : 0;
	
	if($( "#name" ).val()==null && $( "#name" ).val()==""){
		alert("中心名称不能为空");
		return false;
	}

	submitter.post( contextPath+"/index/office_add.json", data, function( d )
	{
		alert("添加成功")
	}, "json" );
}

function init_editors()
{
	$( "div[contenteditable='true']" ).each( function()
	{
		if( _editor.indexOf( $( this ).attr( "id" ) ) == -1 )
			_editor.push( $( this ).attr( "id" ) );
	} );
}

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
	else if( e.ctrlKey && e.shiftKey && e.which == 70 )// ctrl+shift+f
	{
		var edt = $( document.activeElement );
		var id = edt.attr( "id" );

		if( _editor.indexOf( id ) > -1 )
		{
			edt.html( htmlfilter( edt.html() ) );
		}
	}
}
